/**
 * @Date 2023-02-21 14:00:07
 * @Remark
 */

// lib
// import moment from "moment";
// config
// script & methods & public
import toast from "components/toast";
// import timeUtils from "utils/timeUtils";
// store
import { state } from "./state";
// service
import {
  addShelfService,
  deleteShelfService,
  editShelfService,
  getRoomNoPageService,
  getShelfPageService,
} from "biz/Books/service";
import { ReqShelfAddPage, ReqShelfPage } from "biz/Books/api";
// interface

export async function getShelfPage() {
  const sp = state.getSearch();
  const params: ReqShelfPage = {
    pageNum: state.page,
    pageSize: state.size,
    ...sp,
  };
  state.loading = true;
  const result = await getShelfPageService(params);
  state.loading = false;
  if (result.payload) {
    state.total = result.payload.total;
    state.list = result.payload.list;
  }
}

export async function addShelf(form: any) {
  const params: ReqShelfAddPage = {
    ...form,
  };
  state.editModalLoading = true;
  const result = await addShelfService(params);
  state.editModalLoading = false;
  if (result.payload) {
    toast.success("添加成功");
    state.editModal = false;
    getShelfPage();
  }
}

export async function updateShelf(form: any, shelfId: number) {

  // 调用服务
  state.editModalLoading = true;
  const result = await editShelfService({
    shelfId,
    ...form,
  });
  state.editModalLoading = false;
  // 成功
  if (result.payload) {
    toast.success("编辑成功");
    state.editModal = false;
    getShelfPage();
  }
}
//不分页列表
export async function getRoomLongPage() {
  state.loading = true;
  const result = await getRoomNoPageService({});
  state.loading = false;
  if (result.payload) {
    state.longList = result.payload;
  }
}

export async function delShelf(id: number) {
  const result = await deleteShelfService({ shelfId: id });
  if (result.payload) {
    toast.success("删除成功");
    state.setSearch();
    getShelfPage();
  }
}

export async function didMount() {
  getRoomLongPage();
  getShelfPage();
}
